Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

preq

Package Overview
Dependencies
Maintainers
3
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

preq

Yet another promising request wrapper

  • 0.4.12
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.4K
increased by12.05%
Maintainers
3
Weekly downloads
 
Created
Source

preq Build Status

Yet another promising request wrapper.

Features

  • ES6 Promise-based, using the excellent bluebird by default
  • Robustness: retry, timeout and connect timeout support
  • Proper error handling: All errors (incl. HTTP responses with code >= 400) throw exceptions, and can be caught with Promise.catch(). This ensures that all errors are handled, and often cleans up the code by clearly separating out error handling. The HTTPError instance has all the properties of a normal response.

Installation

npm install preq

Usage

var preq = require('preq');

return preq.get({                   // or preq.request({ method: 'get', .. })
    uri: 'http://google.com/',
    headers: {
        'x-foo': 'bar'
    },
    query: {
        q: 'foo'
    },
    // body for POSTs or PUTs, can be object (serialized to JSON), Buffer or String
})
.then(function(res) {
    /**
     * { 
     *   status: 200,
     *   headers: { 
     *     date: 'Sat, 21 Feb 2015 01:47:40 GMT' // , ...
     *   },
     *   body: '<!doctype html>...</html>' // or object if json
     * }
     */
})
.catch(function(err) {
    // Any response with HTTP status >= 400
    // err is HTTPError with same properties as response above
});

preq-specific parameters / methods

preq passes through most options directly to request, so see its documentation for advanced options.

Additionally, it defines or modifies these request options:

  • method: Lowercase HTTP verbs. Automatically set by the verb methods (preq.get(), .post() etc).
  • uri: use uri, not url.
  • query: query string parameters
  • body: String, Buffer or Object. If an object is supplied, the serialization depends on the content-type header. Supported:
    • application/json.*
    • multipart/form-data
    • application/x-www-form-urlencoded
  • retries: Maximum number of retries. Exponential back-off is used between retries.
  • timeout: Total request timeout.
  • connectTimeout: Maximum time to establish a TCP connection to the host, in ms. Useful to quickly fail if a node is unreachable at the network level, without waiting for the full timeout duration.

Also see the tests for usage examples.

FAQs

Package last updated on 18 Oct 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc